package com.t2t.top.framework.diamond;

import com.t2t.top.framework.util.ResourceUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;

import java.util.Properties;

/**
 * Created by yangpengfei on 2016/9/5.
 */
public class Env {
    private static final Logger logger = LoggerFactory.getLogger(Env.class);

    public static final String DIAMOND_DEFAULTGROUP_PROPERTY = "diamond.defaultGroup";
    public static final String DIAMOND_DEFAULTGROUP_PORT = "diamond.defaultPort";
    public static final String DIAMOND_DEFAULTGROUP_TIMEOUT = "diamond.defaultTimeout";
    private static final Properties defaultEnv = new Properties();

    static {
        Resource envResource = ResourceUtils.getResource("classpath:env.properties");
        if (envResource != null) {
            try {
                Properties de = PropertiesUtils.toProperties(envResource.getInputStream());
                defaultEnv.putAll(de);
            } catch (Exception e) {
                logger.error("读取失败", e);
            }
        }
    }

    protected static String getDefaultGroupId() {
        return defaultEnv.getProperty(DIAMOND_DEFAULTGROUP_PROPERTY);
    }

    protected static Integer getDefalutPort() {
        Integer port = 80;
        if (defaultEnv.get(DIAMOND_DEFAULTGROUP_PORT) != null) {
            port = Integer.parseInt(defaultEnv.getProperty(DIAMOND_DEFAULTGROUP_PORT));
        }
        return port;
    }

    protected static Integer getDefalutTimeout() {
        Integer port = 5000;
        if (defaultEnv.get(DIAMOND_DEFAULTGROUP_TIMEOUT) != null) {
            port = Integer.parseInt(defaultEnv.getProperty(DIAMOND_DEFAULTGROUP_TIMEOUT));
        }
        return port;
    }

}
